//
//  CHNavigationController.m
//  CarFile
//
//  Created by 陈浩 on 2017/3/20.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHNavigationController.h"

@interface CHNavigationController ()

@end

@implementation CHNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置导航栏属性
    [self setUpNavigationBarAppearance];
}

#pragma mark - 设置全局的导航栏属性
- (void)setUpNavigationBarAppearance
{
    
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    NSDictionary *textAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    [navigationBarAppearance setTitleTextAttributes:textAttributes];
    navigationBarAppearance.tintColor = [UIColor whiteColor];
    // 加载图片
    UIImage *image = [self imageWithColor:[UIColor colorWithHexString:@"098edd" alpha:1.0f]];
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:0.5 topCapHeight:0.5];
    [navigationBarAppearance setBackgroundImage:newImage forBarMetrics:UIBarMetricsDefault];
    //    navigationBarAppearance.barTintColor = SetColor(80, 189, 203);
}


- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); //宽高 1.0只要有值就够了
    UIGraphicsBeginImageContext(rect.size); //在这个范围内开启一段上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);//在这段上下文中获取到颜色UIColor
    CGContextFillRect(context, rect);//用这个颜色填充这个上下文
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//从这段上下文中获取Image属性,,,结束
    UIGraphicsEndImageContext();
    
    return image;
}




#pragma mark 监听导航控制器推出新控制器
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{

    
    // 如果是导航控制器的第一个控制器，不需要添加返回按钮
    NSInteger currentCount = self.childViewControllers.count;
  
    
    if (currentCount > 0) {
        
        viewController.hidesBottomBarWhenPushed = YES;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.bounds = CGRectMake(0, 0, 20  , 15);
        [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    }
    
    [super pushViewController:viewController animated:animated];
    
    
    
}




-(void)back{
    
    
    [self popViewControllerAnimated:YES];
}



#pragma mark - UIStatusBar
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end
